#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.server.controller;

import java.util.List;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oauth.common.server.OAuthGwtController;
import com.oauth.common.server.beans.OAuthTokenBean;
import com.oauth.components.client.beans.OAuthUrl;
import ${package}.client.beans.HttpQueryString;
import ${package}.client.beans.Mail;
import ${package}.client.beans.YahooModel;
import ${package}.client.rpc.YahooRemoteService;
import ${package}.server.beans.YahooServiceBean;
import ${package}.server.constants.YahooConstants;
import ${package}.server.service.YahooService;

public class YahooGwtController extends OAuthGwtController implements
		YahooRemoteService {

	private static final long serialVersionUID = -5758464341967618224L;
	private OAuthProvider oauthProvider; 
	public void initialise(){
		oauthProvider = new CommonsHttpOAuthProvider(
                "https://api.login.${artifactId}.com/oauth/v2/get_request_token",
                "https://api.login.${artifactId}.com/oauth/v2/get_token" ,
                "https://api.login.${artifactId}.com/oauth/v2/request_auth");
    	
		setTokenCookie(YahooConstants.YAHOO_TOKEN_COOKIE);
		setTokenSecretCookie(YahooConstants.YAHOO_TOKEN_SECRETCOOKIE);
	}
	@Override
	public OAuthUrl getSignInUrl() {
		OAuthUrl oauthUrl = new OAuthUrl();
		OAuthConsumer consumer = getOAuthConsumer();
		String authUrl = null;
		try {
			authUrl = oauthProvider.retrieveRequestToken(consumer,YahooConstants.CALLBACK_URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		OAuthTokenBean oauthTokenBean = new OAuthTokenBean();
		// Create cookies.
		oauthTokenBean.setToken(consumer.getToken());
		oauthTokenBean.setTokenSecret(consumer.getTokenSecret());
		createTokenCookies(oauthTokenBean);

		oauthUrl.setOAuthUrl(authUrl);
		return oauthUrl;
	}

	private OAuthConsumer getOAuthConsumer(){
		return new CommonsHttpOAuthConsumer(getConsumerKey(), getConsumerSecret());
	}
	
	@Override
	public YahooModel getYahooMails(HttpQueryString queryString){
		YahooModel modelAndView = new YahooModel();
		try{
			YahooServiceBean accessToken = getAccessToken(queryString);
			if (accessToken == null) {
				modelAndView.setLoginView(true);
			} else {
			OAuthConsumer consumer = getOAuthConsumer();
			System.out.println("Got access token from db");
			consumer.setTokenWithSecret(accessToken.getAccessToken(),accessToken.getAccessTokenSecret());

			JSONArray jsonObjSend = null;
			JSONObject data = YahooService.getData(consumer, jsonObjSend);
			System.out.println(data.toString());
			
			List<Mail> mailList = getYahooMail(data);
			modelAndView.setMailList(mailList);
			modelAndView.setLoginView(false);// go to home view
			}
		}catch (Exception e) {
			e.printStackTrace();
		} 
		
		return modelAndView;
	}
	
	private List<Mail> getYahooMail(JSONObject pData) {
		// TODO Auto-generated method stub
		return null;
	}
	private YahooServiceBean getAccessToken(HttpQueryString queryString) {
		YahooServiceBean token = null;
		try {
			token = (YahooServiceBean) getToken();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		//got token from db.
		if( token != null){
			return token;
		}
		
		OAuthTokenBean oauthTokenBean = getOAuthTokenFromCookie();
		if(oauthTokenBean != null){
			OAuthConsumer consumer = getOAuthConsumer();
		
			//mOAuthHelper.getOAuthParametersFromCallback(queryString.getQueryString(), oauthParameters);

			consumer.setTokenWithSecret(oauthTokenBean.getToken(), oauthTokenBean.getTokenSecret());
			// Get access token .
			try{
			oauthProvider.retrieveAccessToken(consumer, queryString.getQueryString());

			YahooServiceBean ${artifactId}ServiceBean = new YahooServiceBean();
			${artifactId}ServiceBean.setAccessToken(consumer.getToken());
			${artifactId}ServiceBean.setAccessTokenSecret(consumer.getTokenSecret());
			${artifactId}ServiceBean.setMemberId(getMemberId());
			persistToken(${artifactId}ServiceBean); //persist in db for future use.
 			deleteTokenCookies();
 			return ${artifactId}ServiceBean;//delete request tokens from cookies.
			} catch (Exception e) {
				// Redirect to login page.
				e.printStackTrace();
				deleteTokenCookies();
				return null;
			}
		}
		return null;
	}

	@Override
	protected Class<YahooServiceBean> getServiceBeanClass() {
		return YahooServiceBean.class;
	}
		
}
